package com.sensePlusVR.activity.user;

import org.xutils.x;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.sensePlusVR.activity.listener.AEditTextCallBack;
import com.sensePlusVR.base.BaseActivity;
import com.sensePlusVR.bean.ChangePasswordReqDto;
import com.sensePlusVR.main.R;
import com.sensePlusVR.net.NetRequestManager;
import com.sensePlusVR.utils.AESSecurityUtil;
import com.sensePlusVR.utils.AppManager;
import com.sensePlusVR.utils.Constant;
import com.sensePlusVR.utils.EditTextUtils;
import com.sensePlusVR.utils.MyLog;
import com.sensePlusVR.utils.StringUtils;
import com.sensePlusVR.widget.EditTextDeleteAll;

/**
 * 
 * @author: Loren
 * @create: 2016-8-10下午2:50:22
 * @desc: 重置密码
 * 
 */
@SuppressLint("InflateParams")
@ContentView(R.layout.activity_reset_password)
public class ResetPwdActivity extends BaseActivity implements OnClickListener,
		OnCheckedChangeListener {

	private final static String TAG = ResetPwdActivity.class.getSimpleName();

	/**
	 * 主题view
	 */
	
	@ViewInject(R.id.title_bar_left_ib)
	private ImageButton title_bar_left_ib;
	
	@ViewInject(R.id.title_bar_right_tv)
	private TextView title_bar_right_tv;
	
	@ViewInject(R.id.title_bar_title_tv)
	private TextView txtTitle;
	
	@ViewInject(R.id.title_bar_right_tv)
	private TextView txtRight;

	/**
	 * 原密码
	 */
	@ViewInject(R.id.reset_password_old)
	private EditTextDeleteAll edtTxtOldPwd;
	/**
	 * 新密码
	 */
	@ViewInject(R.id.reset_password_new)
	private EditTextDeleteAll edtTxtNewPwd;

	@ViewInject(R.id.reset_password_new_linear)
	private LinearLayout reset_password_new_linear;
	/**
	 * 新密码确认
	 */
	@ViewInject(R.id.reset_password_confirm)
	private EditTextDeleteAll edtTxtConfirmPwd;

	@ViewInject(R.id.reset_password_confirm_linear)
	private LinearLayout reset_password_confirm_linear;

	@ViewInject(R.id.reset_password_show_cb)
	private CheckBox reset_password_show_cb;// 条款选择框

	@ViewInject(R.id.reset_pwd_old_delete_iv)
	private ImageView reset_pwd_old_delete_iv;

	@ViewInject(R.id.reset_pwd_new_delete_iv)
	private ImageView reset_pwd_new_delete_iv;

	@ViewInject(R.id.reset_pwd_confirm_delete_iv)
	private ImageView reset_pwd_confirm_delete_iv;
	
//	private ImageView reset_pwd_del_iv;
	

	/**
	 * 原密码
	 */
	static String passwordOld;
	/**
	 * 新密码
	 */
	static String passwordNew;
	/**
	 * 新密码确认
	 */
	static String passwordNewConfirm;

	/**
	 * 重置密码请求参数
	 */
	private ChangePasswordReqDto resetPwdReqDto;
	
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public void setMyContentView() {
		x.view().inject(this);
	}

	@Override
	public void initView() {
		super.initView();
		AppManager.getInstance().addActivity(this);
		
		edtTxtOldPwd.setOnAEditTextCallBack(editTextCallBack);
		edtTxtNewPwd.setOnAEditTextCallBack(editTextCallBack);
		edtTxtConfirmPwd.setOnAEditTextCallBack(editTextCallBack);
		
		title_bar_left_ib.setOnClickListener(this);
		title_bar_right_tv.setOnClickListener(this);
		reset_pwd_old_delete_iv.setOnClickListener(this);
		reset_pwd_new_delete_iv.setOnClickListener(this);
		reset_pwd_confirm_delete_iv.setOnClickListener(this);
		
		reset_password_show_cb.setOnCheckedChangeListener(this);
		
		//promptResetPwd();

	}

//	private void promptResetPwd() {
//		Integer locked = MySharedPreferences.getInteger2(this,
//				MySharedPreferences.NAME_NOT_CLEAR,
//				Constant.LOGIN_SUCCESS_LOCKED);
//		MyLog.d(TAG, "locked : " + locked);
//		if (locked == 1) {
//			setClickble(false);
//			//改变顶部返回图标
//			title_bar_left_ib.setImageResource(R.drawable.nav_closeicon_selector);
//			//提示修改密码悬浮View
//			final ViewGroup over_reset_password = (ViewGroup) LayoutInflater.from(this).inflate(R.layout.over_reset_password, null);
//			addContentView(over_reset_password, new LayoutParams(-1, -1));
//			
//			reset_pwd_del_iv = (ImageView) over_reset_password.findViewById(R.id.reset_pwd_del_iv);
//			reset_pwd_del_iv.setOnClickListener(new OnClickListener() {
//				
//				@Override
//				public void onClick(View v) {
//					
//					((ViewGroup)over_reset_password.getParent()).removeView(over_reset_password);
//					setClickble(true);
//					
//				}
//			});
//		}
//	}
	
//	private void setClickble(boolean isClick){
//		title_bar_left_ib.setClickable(isClick);
//		title_bar_right_tv.setClickable(isClick);
//		reset_pwd_old_delete_iv.setClickable(isClick);
//		reset_pwd_new_delete_iv.setClickable(isClick);
//		reset_pwd_confirm_delete_iv.setClickable(isClick);
//		
//		edtTxtOldPwd.setEnabled(isClick);
//		edtTxtNewPwd.setEnabled(isClick);
//		edtTxtConfirmPwd.setEnabled(isClick);
//		
//		reset_password_show_cb.setClickable(isClick);
//	}

	@Override
	public void initData() {
		super.initData();
		// 界面名称-重设密码
		txtTitle.setText(R.string.modify_password);
		txtRight.setText(R.string.save);
	}
	
	
	private AEditTextCallBack editTextCallBack = new AEditTextCallBack() {
		
		@Override
		public void onDeleteVisible(View view, boolean isVisible) {
			
			switch (view.getId()) {
			
			case R.id.reset_password_old:
				
				if(isVisible){
					reset_pwd_old_delete_iv.setVisibility(View.VISIBLE);
				}else{
					reset_pwd_old_delete_iv.setVisibility(View.INVISIBLE);
				}
				
				break;
			
			case R.id.reset_password_new:
				
				if(isVisible){
					reset_pwd_new_delete_iv.setVisibility(View.VISIBLE);
				}else{
					reset_pwd_new_delete_iv.setVisibility(View.INVISIBLE);
				}
				
				break;
			case R.id.reset_password_confirm:
				if(isVisible){
					reset_pwd_confirm_delete_iv.setVisibility(View.VISIBLE);
				}else{
					reset_pwd_confirm_delete_iv.setVisibility(View.INVISIBLE);
				}
				break;

			}
			
		}
		
		public void onBottomTipVisible(View view, boolean isVisible) {
			
			switch (view.getId()) {
			case R.id.reset_password_new:
				if(isVisible){
					reset_password_new_linear.setVisibility(View.VISIBLE);
				}else{
					reset_password_new_linear.setVisibility(View.INVISIBLE);
				}
				break;
			case R.id.reset_password_confirm:
				if(isVisible){
					reset_password_confirm_linear.setVisibility(View.VISIBLE);
				}else{
					reset_password_confirm_linear.setVisibility(View.INVISIBLE);
				}
				break;

			}
			
		};
		
	};
	
	

	@Override
	public void onClick(View v) {
		switch (v.getId()) {

		case R.id.title_bar_left_ib: {
			onBackPressed();
			break;
		}

		// 确认按钮
		case R.id.title_bar_right_tv: {
			// 画面项目check OK
			if (getCheckResult()) {
				resetPwdReqDto = new ChangePasswordReqDto();
				// 新密码AES加密
				try {
					char[] encrypte = AESSecurityUtil.encrypt(passwordNew,
							Constant.KEY_AES);
					resetPwdReqDto.setNewPassword(encrypte);
					Log.d(TAG, "new password...");
				} catch (Exception e) {
					e.printStackTrace();
				}
				// 旧密码AES加密
				try {
					char[] encrypte = AESSecurityUtil.encrypt(passwordOld,
							Constant.KEY_AES);
					resetPwdReqDto.setOrgPassword(encrypte);
					Log.d(TAG, "old password...");
				} catch (Exception e) {
					e.printStackTrace();
				}

				// 发送网络请求
				NetRequestManager.startResetPwdReqApi(ResetPwdActivity.this,
						resetPwdReqDto);
			}
			break;
		}
		
		case R.id.reset_pwd_old_delete_iv:
			edtTxtOldPwd.setText("");
			break;
		case R.id.reset_pwd_new_delete_iv:
			edtTxtNewPwd.setText("");
			break;
		case R.id.reset_pwd_confirm_delete_iv:
			edtTxtConfirmPwd.setText("");
			break;
		}
	}

	/**
	 * 画面数据检查
	 * 
	 * @return
	 */
	private boolean getCheckResult() {
		// 原密码
		passwordOld = EditTextUtils.getText(edtTxtOldPwd);
		if (StringUtils.isEmpty(passwordOld)) {
			EditTextUtils.setFocusable(edtTxtOldPwd);
			Toast.makeText(getApplicationContext(), R.string.old_pwd_empty,
					Toast.LENGTH_SHORT).show();
			return false;
		}
		// 密码长度判断
		if (passwordOld.length() < Constant.NUMBER_6
				|| passwordOld.length() > Constant.NUMBER_15) {
			Toast.makeText(getApplicationContext(), R.string.pwd_min_length,
					Toast.LENGTH_SHORT).show();
			return false;
		}

		// 密码
		passwordNew = EditTextUtils.getText(edtTxtNewPwd);
		// 密码确认
		passwordNewConfirm = EditTextUtils.getText(edtTxtConfirmPwd);
		if (StringUtils.isEmpty(passwordNew)) {
			MyLog.d(TAG, "passwordNew ========== " + passwordNew);
			EditTextUtils.setFocusable(edtTxtNewPwd);
			Toast.makeText(getApplicationContext(), R.string.new_pwd_empty,
					Toast.LENGTH_SHORT).show();
			return false;
		}
		// 密码长度判断
		if (passwordNew.length() < Constant.NUMBER_6
				|| passwordNew.length() > Constant.NUMBER_15
				|| !StringUtils.isNumAndCode(passwordNew)) {
			Toast.makeText(getApplicationContext(), R.string.pwd_min_length,
					Toast.LENGTH_SHORT).show();
			return false;
		}

		// 密码原始密码和新密码是否一致
		if (passwordNew.equals(passwordOld)) {
			Toast.makeText(getApplicationContext(), R.string.pwd_same,
					Toast.LENGTH_SHORT).show();
			return false;
		}

		// 判断重复密码是否有一致
		if (!passwordNew.equals(passwordNewConfirm)) {
			edtTxtConfirmPwd.getText().clear();
			EditTextUtils.setFocusable(edtTxtConfirmPwd);
			Toast.makeText(getApplicationContext(), R.string.confirm_pwd_wrong,
					Toast.LENGTH_SHORT).show();
			return false;
		}
		// check通过
		return true;
	}

	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

		MyLog.d(TAG, "isChecked : " + isChecked);

		if (isChecked) {
			edtTxtOldPwd
					.setTransformationMethod(HideReturnsTransformationMethod
							.getInstance());
			if (checkInputData(edtTxtOldPwd)) {
				edtTxtOldPwd.setSelection(edtTxtOldPwd.getText().toString()
						.length());
			}

			edtTxtNewPwd
					.setTransformationMethod(HideReturnsTransformationMethod
							.getInstance());
			if (checkInputData(edtTxtNewPwd)) {
				edtTxtNewPwd.setSelection(edtTxtNewPwd.getText().toString()
						.length());
			}

			edtTxtConfirmPwd
					.setTransformationMethod(HideReturnsTransformationMethod
							.getInstance());
			if (checkInputData(edtTxtConfirmPwd)) {
				edtTxtConfirmPwd.setSelection(edtTxtConfirmPwd.getText()
						.toString().length());
			}

		} else {

			edtTxtOldPwd.setTransformationMethod(PasswordTransformationMethod
					.getInstance());
			if (checkInputData(edtTxtOldPwd)) {
				edtTxtOldPwd.setSelection(edtTxtOldPwd.getText().toString()
						.length());
			}

			edtTxtNewPwd.setTransformationMethod(PasswordTransformationMethod
					.getInstance());
			if (checkInputData(edtTxtNewPwd)) {
				edtTxtNewPwd.setSelection(edtTxtNewPwd.getText().toString()
						.length());
			}

			edtTxtConfirmPwd
					.setTransformationMethod(PasswordTransformationMethod
							.getInstance());
			if (checkInputData(edtTxtConfirmPwd)) {
				edtTxtConfirmPwd.setSelection(edtTxtConfirmPwd.getText()
						.toString().length());
			}

		}

	}

	private boolean checkInputData(EditText editText) {
		if (StringUtils.isEmpty(EditTextUtils.getText(editText))) {
			return false;
		}
		return true;
	}

	@Override
	public void onBackPressed() {
		super.onBackPressed();
//		boolean isExist = AppManager.getInstance().isActivityExist(MainActivity.class);
//		MyLog.d(TAG, "isExist : " + isExist);
//		if(!isExist){
//			MyPageJump.pageJump(this, MainActivity.class);
//		}
		AppManager.getInstance().finishActivity(this);
	}
}
